package maquinavotar.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import maquinavotar.dto.CandidateDTO;
import maquinavotar.dao.CandidatesDAOImplXml;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import maquinavotar.MaquinaVotarApp;

public class InitialScreen extends JPanel {
  
    private static InitialScreen INSTANCE = null;
    
    private InitialScreen() {}
            
    private synchronized static void createInstance(final MaquinaVotarApp app) {
        
        INSTANCE = new InitialScreen();
        INSTANCE.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Ingrese tarjeta"));
        INSTANCE.setLayout(new GridBagLayout());
                
        JButton btn_vot = new JButton("Ingresar tarj. votante", new ImageIcon("imgs/card-blue.png"));
        btn_vot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                app.showScreen(1,0);
            }
        });
        INSTANCE.add(btn_vot);
        
        JButton btn_adm = new JButton("Ingresar tarj. administrador", new ImageIcon("imgs/card-gold.png"));
        btn_adm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                CandidatesDAOImplXml c = new CandidatesDAOImplXml();
                app.cands = (ArrayList<CandidateDTO>) c.loadCandidatos("data_io/candidatos.xml");
                if(app.cands == null)
                    JOptionPane.showMessageDialog(null, "Error al cargar los candidatos.", "Carga de datos", JOptionPane.WARNING_MESSAGE);
                else JOptionPane.showMessageDialog(null, "Carga de candidatos finalizada exitosamente.", "Carga de datos", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        INSTANCE.add(btn_adm);
    }

    public static InitialScreen getInstance(final MaquinaVotarApp app) {
        if (INSTANCE == null)
            createInstance(app);
        return INSTANCE;
    }
    
    /* IMAGEN DE FONDO ---> si se descomenta esto, se visualiza una imagen de fondo en el panel
     * (me gustó más sin nada, onda minimalista)
    @Override
    public void paint(Graphics g) {
        Image img = (new ImageIcon("imgs/card-background.png")).getImage();
        if (img != null) {
            g.drawImage(img, 0, 0, getWidth(), getHeight(), INSTANCE);
            setOpaque(false);
        } else {
            setOpaque(true);
        }
        super.paint(g);
    }
    */
}
